package ac.manchester.cs.afzal.autoservd.Descriptors;

import java.io.IOException;
import java.util.Vector;
import java.util.Properties;

import java.util.StringTokenizer;

import ac.manchester.cs.afzal.autoservd.file_handler.File_Manager;
import ac.manchester.cs.afzal.autoservd.regular_expressions.String_Manipulator;
import ac.manchester.cs.afzal.autoservd.vector_handler.Vector_Builder;
import java.io.FileInputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hamafz
 */
public class Descriptors_Ngrams
{

    private Vector stopwords = new Vector();
    private String PENN_FILES_LOCATION = "PENN_FILES_LOCATION";
    private String STOPWORDS_LOCATION = "STOPWORDS_LOCATION";
    static private SQL_Handler_Descriptors sqlHandler2;
    private File_Manager fileManager = new File_Manager();
    private String_Manipulator strMan = new String_Manipulator();
    private Vector_Builder vectorBuilder = new Vector_Builder();
    private Properties props = new Properties();

    /**
     *
     */
    public Descriptors_Ngrams()
    {
        this.read_stopwords(STOPWORDS_LOCATION);
        try
        {
            props.load(new FileInputStream("AutoServD.properties"));
            PENN_FILES_LOCATION = props.getProperty(PENN_FILES_LOCATION);
            STOPWORDS_LOCATION = props.getProperty(STOPWORDS_LOCATION);
        }
        catch (IOException ex)
        {
            Logger.getLogger(Descriptors_Ngrams.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Generate n-grams for all service descriptors and fill in the respective table
     */
    public void make_ngrams_service_descriptors()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "___bioinformatics_service_descriptors_ngrams"
                });

        Vector descriptors_ids = sqlHandler2.get_values_from_dictionary();
        Vector descriptors = sqlHandler2.get_descriptors();

        Vector final_vector_descriptors = new Vector();

        for (int i = 0; i < descriptors.size(); i++)
        {
            String descriptor = descriptors.elementAt(i).toString();
            String descriptor_id = descriptors_ids.elementAt(i).toString();

            Vector<String> descriptor_tokens = vectorBuilder.string_to_vector(descriptor, " ");
            Vector<String> descriptor_profile = vectorBuilder.term_profiles(descriptor_tokens);

            for (String ngram : descriptor_profile)
            {
                ngram = ngram.trim();
                ngram = ngram.toLowerCase();
                if (final_vector_descriptors.contains(ngram))
                {
                    int freq = sqlHandler2.get_count_of_descriptors(ngram);
                    int ngram_id = sqlHandler2.get_ngram_id();
                    freq = freq + 1;
                    sqlHandler2.update_table_ngrams("___bioinformatics_service_descriptors_ngrams", ngram_id, "Freq", Integer.toString(freq));
                    System.out.println("Freq");
                }
                else
                {
                    if (this.is_ngram_fit(ngram))
                    {
                        final_vector_descriptors.add(ngram);
                        String[] doc_table1 =
                        {
                            "Ngram", "Service_Descriptor_ID", "Freq"
                        };
                        String[] doc_table_value1 =
                        {
                            ngram, descriptor_id, "1"
                        };
                        sqlHandler2.insert_table("___bioinformatics_service_descriptors_ngrams", doc_table1, doc_table_value1);
                    }
                }
            }
        }

    }

    /**
     *
     */
    public void make_ngrams_sentence_descriptors()
    {
        sqlHandler2.reset_tables(new String[]
                {
                    "__sentence_descriptors_ngrams_b"
                });

        Vector<Integer> d_sen_ids = sqlHandler2.get_distinct_sentences_from_descriptors();
        Vector<Integer> descriptor_ids;
        Vector<String> filtered_descriptors;

        for (Integer d_sen_id : d_sen_ids)
        {
            filtered_descriptors = sqlHandler2.get_descriptors_per_distinct_sentence(d_sen_id);
            descriptor_ids = sqlHandler2.get_sen_ids();

            Vector final_vector_nps;

            for (int i = 0; i < filtered_descriptors.size(); i++)
            {
                String filtered_descriptor = filtered_descriptors.elementAt(i);
                String descriptor_id = descriptor_ids.elementAt(i).toString();

                final_vector_nps = new Vector();

                Vector<String> descriptor_tokens = vectorBuilder.string_to_vector(filtered_descriptor, " ");
                Vector<String> descriptor_profile = vectorBuilder.term_profiles(descriptor_tokens);

                for (String ngram : descriptor_profile)
                {
                    ngram = ngram.trim();
                    ngram = ngram.toLowerCase();

                    if (final_vector_nps.contains(ngram))
                    {
                        int freq = sqlHandler2.get_count_of_descriptors(ngram, d_sen_id);
                        int ngram_id = sqlHandler2.get_ngram_id();
                        freq = freq + 1;
                        sqlHandler2.update_table_ngrams("__sentence_descriptors_ngrams_b", ngram_id, "Freq", Integer.toString(freq));
                    }
                    else
                    {
                        if (is_ngram_fit(ngram))
                        {
                            final_vector_nps.add(ngram);
                            String[] doc_table1 =
                            {
                                "Value", "Descriptor_ID", "Freq", "Sentence_ID"
                            };
                            String[] doc_table_value1 =
                            {
                                ngram, descriptor_id, "1",
                                Integer.toString(d_sen_id)
                            };
                            sqlHandler2.insert_table("__sentence_descriptors_ngrams_b", doc_table1, doc_table_value1);
                        }
                    }
                }
            }
        }
    }

    /**
     * 
     * @param count
     */
    protected void init_database(int count)
    {
        sqlHandler2 = new SQL_Handler_Descriptors();
        sqlHandler2.initialize_parameters("jdbc:mysql://localhost:3306/BMC_2009_2", "root", "admin");
        sqlHandler2.connect_db();

    }

    /**
     *
     * @param np
     * @return
     */
    public boolean is_ngram_fit(String np)
    {
        if ((np.length() > 50) || (np.contains("\'")) ||
                (np.length() < 4) || (np.contains("\"")) ||
                (np.contains("http")) || (np.contains("``")) ||
                (np.contains("&lt")) || (np.matches("^[0-9]")) ||
                (np.contains("</")) || (np.contains("@")) ||
                (np.contains("<")) || (np.contains(">")))
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    /**
     * Reads filtered descriptos from  list from Resources\startlist.txt
     * "The naming convention is a bit wierd .. in contrast to Stopwords, it is named as Startlist
     * @param path
     */
    public void read_stopwords(String path)
    {
        String startwords_str = fileManager.fileReader(path).toString();
        StringTokenizer st1 = new StringTokenizer(startwords_str, "\r\n");
        while (st1.hasMoreElements())
        {
            String term = st1.nextToken();
            term = term.trim();
            term = term.toLowerCase();
            stopwords.add(term);
        }
    }

    /**
     * Filters the stopwords from a single string
     * @param element
     * @return
     */
    public String filter_stop_words_from_one_element(String element)
    {
        Vector<String> temp_np = new Vector();
        temp_np = vectorBuilder.string_to_vector(element, " ");
        temp_np.removeAll(stopwords);
        String processed_element = vectorBuilder.vector_to_string(temp_np, " ");
        processed_element.trim();
        processed_element = (processed_element.length() < 4) ? "EMPTY" : processed_element;

        return processed_element;
    }
}
